﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace OSRun
{
    /// <summary>
    /// 窗体
    /// </summary>
    public partial class MainForm : Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 文件读取
        /// </summary>
        /// <param name="filePath"></param>
        private void RunOpenSEES(string filePath)
        {
            //初始化
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "cmd.exe";
            //是否使用操作系统shell启动
            p.StartInfo.UseShellExecute = false;
            //接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardInput = true;
            //由调用程序获取输出信息
            p.StartInfo.RedirectStandardOutput = true;
            //重定向标准错误输出
            p.StartInfo.RedirectStandardError = false;
            //不显示程序窗口
            p.StartInfo.CreateNoWindow = false;
            //启动程序
            p.Start();
            //获得文件夹路径
            string directPath = Path.GetDirectoryName(filePath);
            //获得文件名（包含后缀）
            string fileName = Path.GetFileName(filePath);
            //平移路径
            p.StandardInput.WriteLine(@"cd /d " + directPath);
            //启动运行
            p.StandardInput.WriteLine("opensees " + fileName);
            //等待程序执行完退出进程
            p.WaitForExit();
            p.Close();
        }

        /// <summary>
        /// 事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_DragDrop(object sender, DragEventArgs e)
        {
            foreach (var path in (System.Array)e.Data.GetData(DataFormats.FileDrop))
                this.RunOpenSEES(path.ToString());
        }

        /// <summary>
        /// 事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            { e.Effect = DragDropEffects.All; }
            else { e.Effect = DragDropEffects.None; }
        }

        /// <summary>
        /// 事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainLabel_DragDrop(object sender, DragEventArgs e)
        {
            foreach (var path in (System.Array)e.Data.GetData(DataFormats.FileDrop))
                this.RunOpenSEES(path.ToString());
        }

        /// <summary>
        /// 事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainLabel_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            { e.Effect = DragDropEffects.All; }
            else { e.Effect = DragDropEffects.None; }
        }

        /// <summary>
        /// 关于OSRun
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void aboutOSRunToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HelpForm.Show(new List<string> { "OSRun (OpenSees Running Assitant)",
                   "OpenSees运行辅助工具", "https://gitee.com/civilwilson/OSRun", "2019.12.05"}, 
                   Properties.Resources.Logo);
        }
    }
}
